﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.IO;

namespace WebApplication1
{
    public partial class BookEdit : System.Web.UI.Page
    {
        public string strImg
        {
            set;
            get;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserInfo"] == null || ((Entity.UserInfo)Session["UserInfo"]).UserType != "admin")
            {
                Response.Redirect("~/Search.aspx");
            }

            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                SQLHelper.getRootTags(out dt);
                for (int ix = 0; ix < dt.Rows.Count; ix++)
                {
                    ddlType.Items.Add(new ListItem(dt.Rows[ix]["TagName"].ToString(), dt.Rows[ix]["Id"].ToString()));
                }

                if (Session["BookId"] == null)
                {
                    strImg = "ShowImage.aspx?";
                }
                else
                {
                    int i = Convert.ToInt32(Session["BookId"]);
                    DataTable m_dt = new DataTable();
                    SQLHelper.getBookById(i, out m_dt);
                    int ix = 0;
                    while (ddlType.Items[ix].Value != m_dt.Rows[0]["PK_TagInfo"].ToString())
                    {
                        ix++;
                    }
                    if (ix < ddlType.Items.Count)
                        ddlType.Items[ix].Selected = true;
                    tbxBookName.Text = m_dt.Rows[0]["BookName"].ToString();
                    tbxBookUrl.Text = m_dt.Rows[0]["BookUrl"].ToString();
                    tbxBookDescription.Text = m_dt.Rows[0]["BookDescription"].ToString();
                    strImg = "ShowImage.aspx?id="+i.ToString();
                    imageController.ImageUrl = strImg;
                }
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (tbxBookName.Text == null)
            {
                Response.Write("<script language = javascript>alert('请输入书籍名称');</script>");
                return;
            }
            if (tbxBookUrl.Text == null)
            {
                Response.Write("<script language = javascript>alert('请输入书籍链接');</script>");
                return;
            }
            if (tbxBookDescription.Text == null)
            {
                Response.Write("<script language = javascript>alert('请输入书籍简介');</script>");
                return;
            }
            if (ddlType.SelectedItem == null)
            {
                Response.Write("<script language = javascript>alert('请选择书籍所属标签');</script>");
                return;
            }
            if (Session["BookId"] != null)
            {
                int i = (Int32)Session["BookId"];
                int iresult;
                if (FileUpload_image.HasFile)
                {
                    string strImageFileTypeLimit= ConfigurationManager.AppSettings["ImageFileTypeLimit"].ToString();
                    if (!strImageFileTypeLimit.Contains(Path.GetExtension(FileUpload_image.FileName + ",").ToLower()))
                    {
                        Response.Write("<script>alert('图片文件格式不符!');location.href='BooksManage.aspx';</script>");
                        return;

                    }
                    iresult = SQLHelper.updateBookByIdWithImage(i, tbxBookName.Text, FileUpload_image.FileBytes, tbxBookUrl.Text, tbxBookDescription.Text, Convert.ToInt32(ddlType.SelectedItem.Value),DateTime.Now.ToString());
                }
                else
                {
                    iresult = SQLHelper.updateBookByIdWithoutImg(i, tbxBookName.Text, tbxBookUrl.Text, tbxBookDescription.Text, Convert.ToInt32(ddlType.SelectedItem.Value),DateTime.Now.ToString());
                }

                if (iresult == 1)
                {
                    Session.Remove("NewsId");
                    Response.Write("<script>alert('编辑成功!');location.href='BooksManage.aspx';</script>");
                }
                else
                {
                    Session.Remove("NewsId");
                    Response.Write("<script>alert('编辑失败!');location.href='BooksManage.aspx';</script>");
                }
                
            }
            else
            {
                if (!FileUpload_image.HasFile)
                {
                    Response.Write("<script language = javascript>alert('请上传书籍图片');</script>");
                    return;
                }
                int iresult = SQLHelper.AddBook(tbxBookName.Text, FileUpload_image.FileBytes, tbxBookUrl.Text, tbxBookDescription.Text, Convert.ToInt32(ddlType.SelectedItem.Value),DateTime.Now.ToString());
                if (iresult == 1)
                {
                    Response.Write("<script>alert('新增成功!');location.href='BooksManage.aspx';</script>");
                }
                else
                {
                    Response.Write("<script>alert('新增失败!');location.href='BooksManage.aspx';</script>");
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

            Session.Remove("BookId");
            Response.Redirect("~/BooksManage.aspx");
        }
    }
}